package com.sk.bilibili.view.activity;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.vlayout.main.HeaderAdapter;
import com.sk.bilibili.adapter.vlayout.main.MenuAdapter;
import com.sk.bilibili.adapter.vlayout.main.StateAdapter;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.databinding.ActivityMainBinding;
import com.sk.bilibili.event.MainMenuEvent;
import com.sk.bilibili.model.activity.MainModel;
import com.sk.bilibili.utils.DataUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainModel> {

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
        initOther();
    }

    private void initOther() {
        model.checkNew();
    }

    private HeaderAdapter headerAdapter;
    private StateAdapter stateAdapter;
    private Fragment[] fragments;
    private int lastIndex;

    private void initView() {
        initMenu();
        initFragment();
    }

    private void bindEvent() {
        bind.tvSetting.setOnClickListener(v -> model.showLogout());
        bind.tvTheme.setOnClickListener(v->toast("暂不支持"));
        bind.tvNight.setOnClickListener(v->model.changeThem());
    }

    private void initFragment() {
        fragments = model.createFragment();
        lastIndex = 0;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : fragments) {
            transaction.add(R.id.fl_show, fragment).hide(fragment);
        }
        transaction.show(fragments[lastIndex]).commitAllowingStateLoss();
    }

    private void initMenu() {
        VirtualLayoutManager manager = new VirtualLayoutManager(this);
        bind.rvLeft.setLayoutManager(manager);
        DelegateAdapter adapter = new DelegateAdapter(manager);
        headerAdapter = new HeaderAdapter(this);
        adapter.addAdapter(headerAdapter);
        stateAdapter = new StateAdapter(this);
        adapter.addAdapter(stateAdapter);
        adapter.addAdapter(new MenuAdapter(this, this));
        bind.rvLeft.setAdapter(adapter);
    }

    public void loadSuccess(Member member) {
        headerAdapter.setData(member);
        stateAdapter.setData(member);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleMenuEvent(MainMenuEvent event) {
        bind.dlShow.openDrawer(Gravity.LEFT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    public void onClickMember(Integer id) {
        MemberActivity.start(this, id);
    }

    public void onClickMoney() {
        toast("帮你省钱了");
    }

    public void onClickScan() {
        startActivityForResult(new Intent(this, ScanActivity.class), 2233);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2233 && resultCode == RESULT_OK) {
            model.handleScanRes(data);
        }
    }

    public void scanSuccess() {
        toast("登录成功");
    }

    public void scanError(String message) {
        toast(message);
    }

    public void isOld(Intent downLoad, Integer newVersion) {
        new AlertDialog.Builder(this)
                .setMessage(String.format("有新版本v%d是否更新?",newVersion))
                .setPositiveButton("更新", (dialog, which) -> startActivity(downLoad))
                .setNegativeButton("取消", null).show();
    }

    public void onMenuClick(int pos) {
        bind.dlShow.closeDrawer(Gravity.LEFT);
        if (lastIndex == pos) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastIndex]);
        lastIndex = pos;
        transaction.show(fragments[lastIndex]).commitAllowingStateLoss();
    }

    public void onClickLogout() {
        startActivity(LoginActivity.class,true);
    }

    public void reboot() {
        finish();
    }

}